package com.wgke.bluetooth.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.wgke.bluetooth.tools.BtMsg;
import com.wgke.bluetooth.utils.CommonUtil;

public class SpUtil {


    private static SharedPreferences spUser;
    private static SharedPreferences.Editor editorUser;

    private SpUtil() {
        // Private constructor to prevent instantiation
    }

    private static SharedPreferences getUserSharePreference(Context context) {
        if (spUser == null) {
            spUser = context.getSharedPreferences("UserSp", Context.MODE_PRIVATE);
        }
        return spUser;
    }

    private static SharedPreferences.Editor getUserEditor(Context context) {
        if (editorUser == null) {
            editorUser = getUserSharePreference(context).edit();
        }
        return editorUser;
    }

    public static void clear(Context context) {
        SharedPreferences.Editor editor = getUserEditor(context);
        if (editor != null) {
            editor.clear().commit();
        }
    }

    /**
     * Store time
     */
    public static void put(String key, Object value) {
        SharedPreferences.Editor editor = getUserEditor(CommonUtil.getContext());
        if (editor != null) {
            if (value == null) {
                editor.remove(key);
                editor.commit();
                return;
            }
            editor.putString(key, value.toString()).apply();
        }
    }

    public static String getValue(String key) {
        SharedPreferences sp = getUserSharePreference(CommonUtil.getContext());
        return sp.getString(key, "");
    }

    public static int getInt(String key) {
        SharedPreferences sp = getUserSharePreference(CommonUtil.getContext());
        try {
            return Integer.parseInt(sp.getString(key, "0"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static long getLong(String key) {
        SharedPreferences sp = getUserSharePreference(CommonUtil.getContext());
        try {
            return Long.parseLong(sp.getString(key, "0"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static Boolean getBool(String key) {
        return getBool(key, false);
    }

    public static Boolean getBool(String key, boolean def) {
        SharedPreferences sp = getUserSharePreference(CommonUtil.getContext());
        try {
            if (TextUtils.isEmpty(sp.getString(key, ""))) return def;
            return "true".equals(sp.getString(key, ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void setUsePolicy(boolean usePolicy) {
        put(USE_POLICY, usePolicy);
        aBoolean = usePolicy;
    }

    private static Boolean aBoolean = null;

    public static boolean getUsePolicy() {
        if (aBoolean == null) {
            aBoolean = getBool(USE_POLICY);
        }
        return aBoolean;
    }

    private static Boolean aBoolean2 = null;

    public static boolean getUseParse() {
        if (aBoolean2 == null) {
            aBoolean2 = getBool(USE_PARSE);
        }
        return aBoolean2;
    }

    public static void setUseParse(boolean useParse) {
        put(USE_PARSE, useParse);
        aBoolean2 = useParse;
    }


    public static void setUsePolicyHeader(byte a, byte b) {
        StringBuilder hexString1 = new StringBuilder();
        hexString1.append(String.format("%02X ", a));
        hexString1.append(String.format("%02X ", b));
        put(USE_POLICY_HEADER, hexString1.toString());
    }

    public static byte[] getUsePolicyHeader() {
        String value = getValue(USE_POLICY_HEADER);
        byte[] data = BtMsg.hexStringToByteArray(value);
        if (data == null || data.length != 2) {
            return new byte[]{(byte) 0x5A, (byte) 0xA5};
        }
        return BtMsg.hexStringToByteArray(value);
    }

    public static void setUsePolicyFooter(int usePolicyFooter) {
        put(USE_POLICY_FOOTER, usePolicyFooter);
    }

    public static byte getUsePolicyFooter() {
        int value = getInt(USE_POLICY_FOOTER);
        if (value == 0) return (byte) 0xAA;
        return (byte) value;
    }

    public static void setUsePolicySize(boolean usePolicySize) {
        put(USE_POLICY_SIZE, usePolicySize);
    }

    public static boolean getUsePolicySize() {
        return getBool(USE_POLICY_SIZE, true);
    }

    public static void setUsePolicySum(boolean usePolicySum) {
        put(USE_POLICY_SUM, usePolicySum);
    }

    public static boolean getUsePolicySum() {
        return getBool(USE_POLICY_SUM, true);
    }


    public static final String USE_POLICY = "use_policy";
    public static final String USE_PARSE = "use_parse";
    public static final String USE_POLICY_HEADER = "use_policy_header";
    public static final String USE_POLICY_FOOTER = "use_policy_footer";
    public static final String USE_POLICY_SIZE = "use_policy_size";
    public static final String USE_POLICY_SUM = "use_policy_sum";
}
